package com.siashan.toolkit.sms.yunpian.core;

import com.siashan.toolkit.sms.core.entity.*;
import com.siashan.toolkit.sms.core.executor.AbstractSmsSendExecutor;
import com.siashan.toolkit.sms.core.utils.StringUtils;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringSubstitutor;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 云片短信通知执行器
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class YunpianSmsSendExecutor extends AbstractSmsSendExecutor {

    /**
     * 短信客户端
     */
    private final YunpianClient client;


    /**
     * 云片短信配置
     */
    private YunpianSmsConfig yunpianSmsConfig;

    /**
     * 构造云片短信发送处理
     *
     * @param config 云片短信配置
     */
    public YunpianSmsSendExecutor(YunpianSmsConfig config) {
        super(config);
        this.yunpianSmsConfig = config;
        client = new YunpianClient(config.getApiKey()).init();
    }

    /**
     * 构造云片短信发送处理
     *
     * @param config 云片短信配置
     */
    public YunpianSmsSendExecutor(YunpianSmsConfig config,Map<String, SmsTemplate> templateMap) {
        this(config);
        this.templateMap = templateMap;
    }

    /**
     * 构造云片短信发送处理
     *
     * @param config 云片短信配置
     */
    public YunpianSmsSendExecutor(YunpianSmsConfig config,Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList) {
        this(config,templateMap);
        this.whiteList = whiteList;
    }

    /**
     * 构造云片短信发送处理
     *
     * @param config 云片短信配置
     */
    public YunpianSmsSendExecutor(YunpianSmsConfig config,Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList,SmsFrequencyLimitConfig frequencyLimit) {
        this(config,templateMap,whiteList);
        this.frequencyLimit = frequencyLimit;
    }

    /**
     * 发送批量通知
     *
     * @param smsSendModel 通知参数
     * @return 是否发送成功
     */
    @Override
    public boolean doSend(SmsModel smsSendModel) {
        Map<String, String> params = smsSendModel.getParams();
        String mobileString = StringUtils.join(smsSendModel.getPhones(), ",");
        String text = smsSendModel.getTemplate() ;
        // 如果参数不为空，则替换模板中的参数，如果参数为空，直接取模板
        if (null != params && !params.isEmpty()) {
            StringSubstitutor strSubstitutor = new StringSubstitutor(params);
            text = strSubstitutor.replace(text);
        }


        Map<String, String> data = new HashMap<>(8);
        data.put(YunpianClient.MOBILE, mobileString);
        data.put(YunpianClient.TEXT, text);
        Result<?> result;
        if (smsSendModel.getPhones().size() > 1) {
            result = client.sms().batch_send(data);
        } else {
            result = client.sms().single_send(data);
        }
        boolean succeed = Objects.equals(result.getCode(), 0);
        if (!succeed) {
            log.debug("send fail: {}", result.getMsg());
        }
        return succeed;
    }

    /**
     * 获取通道
     *
     * @return 通道
     */
    @Override
    public SmsChannel getChannel() {
        return SmsChannel.YUNPIAN;
    }

}
